package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baizhi.dao.CmfzGuruMapper;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzGuruService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CmfzGuruServiceImpl implements CmfzGuruService {
    @Autowired
    private CmfzGuruMapper cmfzGuruMapper;
    @Override
    public Map selectCmfzGuruDatagrid(int page, int rows) {
        Map map=new HashMap();
        int count=cmfzGuruMapper.selectCmfzGuruCount();
        int start=(page-1)*rows;
        List<CmfzGuru> cmfzGuruList=cmfzGuruMapper.selectCmfzGuruDatagrid(start,rows);

        map.put("total",count);
        map.put("rows",cmfzGuruList);
        return map;
    }

    @Override
    public void deleteSomeGurus(int[] ids) {
        cmfzGuruMapper.deleteSomeGurus(ids);
    }

    @Override
    public void insertGuru(CmfzGuru cmfzGuru) {
        cmfzGuruMapper.insertGuru(cmfzGuru);
    }

    @Override
    public List<CmfzGuru> selectAllGurus() {
        return cmfzGuruMapper.selectAllGurus();
    }

    @Override
    public Map exportGuruXLS() throws Exception {
        Map map=new HashMap();
        List<CmfzGuru> cmfzGuruList=cmfzGuruMapper.selectAllGurus();
        //标题   表名  导出类型   HSSF xls  XSSF   xlsx
        ExportParams exportParams = new ExportParams("上师表","cmfzGuru", ExcelType.HSSF);
        //导出参数对象
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CmfzGuru.class, cmfzGuruList);

        workbook.write(new FileOutputStream(new File("D://cmfzGuru.xls")));
        map.put("isExport","D://cmfzGuru.xls");
        return map;
    }

    @Override
    public Map importGuruXLS() throws Exception {
        Map map=new HashMap();

        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);

        //图片导入
        //importParams.setNeedSave(true);
        //importParams.setSaveUrl();
        List<CmfzGuru> cmfzGuruList = ExcelImportUtil.importExcel(new File("D://cmfzGuru.xls"), CmfzGuru.class, importParams);

        //    1.读取文件
        FileInputStream fileInputStream=new FileInputStream("D://cmfzGuru.xls");

        //    2.解析流得到工作簿对象
        HSSFWorkbook workbook=new HSSFWorkbook(fileInputStream);

        //    3.获取表
        HSSFSheet sheet = workbook.getSheet("cmfzGuru");

        //    4.获取行 1----最后一行
        int lastRowNum = sheet.getLastRowNum();

//    5.获取数据封装在集合中
        for (int i = 1; i < lastRowNum; i++) {
//      获取行
            CmfzGuru cmfzGuru=new CmfzGuru();
            HSSFRow row = sheet.getRow(i);

//      获取单元格和数据
            //编号
            HSSFCell cell = row.getCell(0);
            double numericCellValue = cell.getNumericCellValue();
            int guruId=(int)numericCellValue;
            cmfzGuru.setGuruId(guruId);

            //姓名
            HSSFCell cell1 = row.getCell(1);
            String guruName = cell1.getStringCellValue();
            cmfzGuru.setGuruName(guruName);

            //图片
            HSSFCell cell2 = row.getCell(2);
            String guruImage = cell2.getStringCellValue();
            cmfzGuru.setGuruImage(guruImage);

            //法号
            HSSFCell cell3 = row.getCell(3);
            String guruNickname = cell3.getStringCellValue();
            cmfzGuru.setGuruNickname(guruNickname);

            //状态
            HSSFCell cell4 = row.getCell(4);
            double numericCellValue1 = cell4.getNumericCellValue();
            int guruStatus=(int)numericCellValue1;
            cmfzGuru.setGuruStatus(guruStatus);

//      封装好的对象放入集合
            cmfzGuruList.add(cmfzGuru);
        }

//    6.添加数据库
        for (CmfzGuru cmfzGuru : cmfzGuruList) {
            try {
                cmfzGuruMapper.insertGuru(cmfzGuru);
                map.put("isImport",true);
            } catch (Exception e) {
                e.printStackTrace();
                map.put("isImport",false);
            }
        }
        return map;
    }


}
